//
//  XKMineMarketController.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/14.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKMineMarketController.h"
#import "XKTradeMarketCell.h"
#import "XKTradeMarketModel.h"
#import "XKTradeDataModel.h"
#import "XKMainNavigationController.h"
#import "XKLoginViewController.h"

static NSString *const XKTradeMarketCellID = @"XKTradeMarketCellReuseIdentifier";

@interface XKMineMarketController ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation XKMineMarketController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self refreshData];
    [self loadCurrentMarketPrice];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beganLoadData:) name:@"BeganLoadMarketData" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endLoadData:) name:@"EndLoadMarketData" object:nil];
    
}

- (void)beganLoadData:(NSNotification *)notification {
    [self refreshData];
}

- (void)endLoadData:(NSNotification *)notification {
    [[JX_GCDTimerManager sharedInstance] cancelTimerWithName:@"marketPriceTimer"];
}


- (void)refreshData {

    
    @weakify(self);
    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:@"marketPriceTimer"
                                                           timeInterval:2.0
                                                                  queue:nil
                                                                repeats:YES
                                                           actionOption:AbandonPreviousAction
                                                                 action:^{
                                                                     @strongify(self);
                                                                     [self loadCurrentMarketPrice];
                                                                 }];
}

#pragma mark - 加载交易大盘数据
- (void)loadCurrentMarketPrice {
    // 应用处于后台时,停止数据请求
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        return;
    }
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isCanOperation"]) {
        return;
    }
    NSLog(@"加载大盘数据");
    

    @weakify(self);
    [APIManager POSTRequestWith:(APIPOSTRequestState_MarketOrders) parameters:@{} successBlock:^(NSDictionary *successDict) {
        XKTradeDataModel *dataModel = [XKTradeDataModel mj_objectWithKeyValues:successDict[@"data"]];
        @strongify(self);
        self.dataArray = dataModel.record;
        [self.tableView reloadData];
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
    
}

- (void)setupUI {
    self.tableView.backgroundColor = WhiteColor;
    self.tableView.scrollEnabled = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.tableView registerClass:[XKTradeMarketCell class] forCellReuseIdentifier:XKTradeMarketCellID];
}


#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    XKTradeMarketCell *cell = [tableView dequeueReusableCellWithIdentifier:XKTradeMarketCellID];
    if (cell == nil) {
        cell = [[XKTradeMarketCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XKTradeMarketCellID];
    }
    cell.recoderModel = self.dataArray[indexPath.row];
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 5*Scale_H;
    }
}

#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    
    XKTradeMarketCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([self.delegate respondsToSelector:@selector(didSelectedCell:price:number:)]) {
        [self.delegate didSelectedCell:cell.recoderModel.type price:cell.recoderModel.price number:[NSString stringWithFormat:@"%zd",(cell.recoderModel.num.integerValue - cell.recoderModel.trade_num.integerValue)]];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
